package cn.com.jrr.core.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.jrr.core.common.ToolUtils;
import cn.com.jrr.core.dao.impl.BaseDaoImpl;
import cn.com.jrr.core.model.DataTableRequest;
import cn.com.jrr.core.model.DataTableResponse;
import cn.com.jrr.core.model.PageModel;
import cn.com.jrr.core.model.Vote;
import cn.com.jrr.core.model.VoteDetail;

@Service
public class VoteDetailService{
	@Resource
	private BaseDaoImpl<VoteDetail> baseDaoImpl;
	@Autowired
	PageModel<VoteDetail> pageModel;

	/**
	 * 查询数据
	 * */
	public DataTableResponse<VoteDetail> getAll(DataTableRequest dataTableRequest,Integer voteid){
		pageModel.setCurrentItem(dataTableRequest.getCurrentItem());
		pageModel.setPageSize(dataTableRequest.getPageSize());
		String sql="select * from votedetail where voteid="+voteid+" order by id desc";
		Object [] obj=new String[]{"id","title","qty"};
		sql=ToolUtils.SearchAndSortHql(sql, dataTableRequest, obj);
		DataTableResponse<VoteDetail> dataTableResponse=baseDaoImpl.findBySql(sql,pageModel,VoteDetail.class);
		return  dataTableResponse;
	}
	/**
	 * 添加数据
	 * */
	public void add(VoteDetail voteDetail){
		baseDaoImpl.saveO(voteDetail);
	}
	/**
	 * 获取一条数据
	 * */
	public VoteDetail get(Integer id){
		return  baseDaoImpl.get(VoteDetail.class, id);
	}
	/**
	 * 修改数据
	 * */
	public void update(VoteDetail voteDetail){
		baseDaoImpl.updateObj(voteDetail);
	}
	/**
	 * 删除数据
	 * */
	public int delete(Integer id){
		return  baseDaoImpl.updateBySql("delete from votedetail where id=?", new Object[]{id});
	}

	/**
	 * 查询数据
	 * */
	public List<VoteDetail> getDetails(Integer voteid){
		String sql="select * from votedetail where voteid=? order by id asc";
		List<VoteDetail> data=baseDaoImpl.findBySql(sql, VoteDetail.class, voteid);
		return  data;
	}
	/**
	 * 投票
	 * */
	public void setQty(Integer qty,Integer id){
		String sql="update votedetail set qty=? where id=?";
		baseDaoImpl.updateBySql(sql, new Object[]{qty,id});
	}


}
